<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="工艺编码" v-bind="validateInfos.number">
            <a-input v-model:value="formData.number" placeholder="请输入工艺编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="生产工艺名称" v-bind="validateInfos.name">
            <a-input v-model:value="formData.name" placeholder="请输入生产工艺名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="反应类型" v-bind="validateInfos.reactionType">
            <a-input v-model:value="formData.reactionType" placeholder="请输入反应类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="12">
          <a-form-item label="重点监控装置" v-bind="validateInfos.riskDeviceId">
	          <j-dict-select-tag v-model:value="formData.riskDeviceId" dictCode="tb_base_risk_device,name,id" placeholder="请选择重点监控装置" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="重点监控单元" v-bind="validateInfos.riskUnitId">
	          <j-dict-select-tag v-model:value="formData.riskUnitId" dictCode="tb_base_risk_unit,risk_unit_name,id" placeholder="请选择重点监控单元" :disabled="disabled"/>
          </a-form-item>
        </a-col> -->
        
        <a-col :span="12">
          <a-form-item label="重点监控装置" v-bind="validateInfos.riskDeviceId">
            <j-dict-select-tag v-model:value="formData.riskDeviceId" dictCode="tb_base_risk_device,name,id" placeholder="请选择所属装置" :disabled="disabled" @change="changeDeviceId"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="重点监控单元" v-bind="validateInfos.riskUnitId">
            <j-dict-select-tag v-model:value="formData.riskUnitId" :dictCode="argument.unitDictCode" placeholder="请选择所属单元" :disabled="argument.riskUnitFlag"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="最终产品" v-bind="validateInfos.finalProduct">
            <a-input v-model:value="formData.finalProduct" placeholder="请输入最终产品" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="最终产品物质形态" v-bind="validateInfos.finalProductMediumForm">
            <a-input v-model:value="formData.finalProductMediumForm" placeholder="请输入最终产品物质形态" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="中间产品" v-bind="validateInfos.intermediateProduct">
            <a-input v-model:value="formData.intermediateProduct" placeholder="请输入中间产品" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="中间产品物质形态" v-bind="validateInfos.intermediateProductForm">
	          <j-dict-select-tag v-model:value="formData.intermediateProductForm" dictCode="mediumForm" placeholder="请选择中间产品物质形态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="工艺危险特点" v-bind="validateInfos.processHazardCharacteristics">
            <a-input v-model:value="formData.processHazardCharacteristics" placeholder="请输入工艺危险特点" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="工艺所在地址" v-bind="validateInfos.processLocation">
            <a-input v-model:value="formData.processLocation" placeholder="请输入工艺所在地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="工艺控制参数" v-bind="validateInfos.processControlParameter">
            <a-input v-model:value="formData.processControlParameter" placeholder="请输入工艺控制参数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="工艺控制方式" v-bind="validateInfos.processControlMode">
            <a-input v-model:value="formData.processControlMode" placeholder="请输入工艺控制方式" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投用日期" v-bind="validateInfos.sserviceTime">
		        <a-date-picker placeholder="请选择投用日期" v-model:value="formData.sserviceTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="重点监控工艺参数" v-bind="validateInfos.processParameter">
            <a-input v-model:value="formData.processParameter" placeholder="请输入重点监控工艺参数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="操作温度" v-bind="validateInfos.operatingTemperature">
            <a-input v-model:value="formData.operatingTemperature" placeholder="请输入操作温度" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="操作压力" v-bind="validateInfos.operatingPressure">
            <a-input v-model:value="formData.operatingPressure" placeholder="请输入操作压力" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否重点监管化工工艺" v-bind="validateInfos.isKeySupervision">
            <j-dict-select-tag type='radio' v-model:value="formData.isKeySupervision" dictCode="yn" placeholder="请选择是否重点监管化工工艺" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="岗位操作人数" v-bind="validateInfos.operatorsNumber">
            <a-input v-model:value="formData.operatorsNumber" placeholder="请输入岗位操作人数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="持证人数" v-bind="validateInfos.cardholdersNumber">
            <a-input v-model:value="formData.cardholdersNumber" placeholder="请输入持证人数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="设计单位" v-bind="validateInfos.designCompany">
            <a-input v-model:value="formData.designCompany" placeholder="请输入设计单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="设计单位资质等级" v-bind="validateInfos.designCompanyAptitudeLevel">
            <a-input v-model:value="formData.designCompanyAptitudeLevel" placeholder="请输入设计单位资质等级" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="工艺系统简况" v-bind="validateInfos.processSystemBrief">
	          <a-textarea v-model:value="formData.processSystemBrief" :rows="4" placeholder="请输入工艺系统简况" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="自动控制措施" v-bind="validateInfos.automaticControlMeasure">
	          <a-textarea v-model:value="formData.automaticControlMeasure" :rows="4" placeholder="请输入自动控制措施" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="资质等级描述" v-bind="validateInfos.qualificationLevelDescription">
	          <a-textarea v-model:value="formData.qualificationLevelDescription" :rows="4" placeholder="请输入资质等级描述" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="状态" v-bind="validateInfos.status">
            <j-dict-select-tag type='radio' v-model:value="formData.status" dictCode="normalOrStop" placeholder="请选择状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上传文件" v-bind="validateInfos.doc">
	          <j-upload v-model:value="formData.doc"  :disabled="disabled" ></j-upload>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上传图片" v-bind="validateInfos.img">
	          <j-image-upload  v-model:value="formData.img" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../TbBasicsTechnology.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    number: '',   
    name: '',   
    reactionType: '',   
    riskDeviceId: '',   
    riskUnitId: '',   
    finalProduct: '',   
    finalProductMediumForm: '',   
    intermediateProduct: '',   
    intermediateProductForm: '',   
    processHazardCharacteristics: '',   
    processLocation: '',   
    processControlParameter: '',   
    processControlMode: '',   
    sserviceTime: '',   
    processParameter: '',   
    operatingTemperature: '',   
    operatingPressure: '',   
    isKeySupervision: '',   
    operatorsNumber: '',   
    cardholdersNumber: '',   
    designCompany: '',   
    designCompanyAptitudeLevel: '',   
    processSystemBrief: '',   
    automaticControlMeasure: '',   
    qualificationLevelDescription: '',   
    status: '',   
    doc: '',   
    img: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    number: [{ required: true, message: '请输入工艺编码!'},],
    name: [{ required: true, message: '请输入生产工艺名称!'},],
    reactionType: [{ required: true, message: '请输入反应类型!'},],
    riskDeviceId: [{ required: true, message: '请输入重点监控装置!'},],
    riskUnitId: [{ required: true, message: '请输入重点监控单元!'},],
    finalProduct: [{ required: true, message: '请输入最终产品!'},],
    processHazardCharacteristics: [{ required: true, message: '请输入工艺危险特点!'},],
    processParameter: [{ required: true, message: '请输入重点监控工艺参数!'},],
    isKeySupervision: [{ required: true, message: '请输入是否重点监管化工工艺!'},],
    operatorsNumber: [{ required: true, message: '请输入岗位操作人数!'},],
    cardholdersNumber: [{ required: true, message: '请输入持证人数!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });
  
  const argument = reactive({
    unitDictCode: "tb_base_risk_unit,risk_unit_name,id",
    checkAcceptFlag: false,
    riskUnitFlag: true
  })

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }
  
  function changeDeviceId(id) {
    if(id){
      formData.riskUnitId = null;
      argument.riskUnitFlag = false;
    }else{
      argument.riskUnitFlag = true;
    }
    argument.unitDictCode = "tb_base_risk_unit,risk_unit_name,id,device_id="+id
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
